package com.it.base.core.system.dept.service.impl;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.it.base.common.view.PageResult;
import com.it.base.core.system.dept.mapper.SysDeptMapper;
import com.it.base.core.system.dept.model.entity.SysDept;
import com.it.base.core.system.dept.model.from.SysDeptSelectListFormDTO;
import com.it.base.core.system.dept.model.from.SysDeptSelectPageListFormDTO;
import com.it.base.core.system.dept.model.from.SysDeptValidatorFormDTO;
import com.it.base.core.system.dept.service.ISysDeptService;
import com.it.base.support.string.StringUtil;

@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements ISysDeptService {

	@Autowired
	private SysDeptMapper sysDeptMapper;

	@Override
	public PageResult<SysDept> selectPageList(@Valid SysDeptSelectPageListFormDTO sysDeptSelectPageListFormDTO) {
		QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();

		if (StringUtil.isNotBlank(sysDeptSelectPageListFormDTO.getDeptName())) {
			queryWrapper.lambda().eq(SysDept::getDeptName, sysDeptSelectPageListFormDTO.getDeptName());
		}

		if (sysDeptSelectPageListFormDTO.getStatus() != null) {
			queryWrapper.lambda().eq(SysDept::getStatus, sysDeptSelectPageListFormDTO.getStatus());
		}

		queryWrapper.lambda().orderByAsc(SysDept::getOrderNum);
		IPage<SysDept> page = sysDeptMapper.selectPage(
				new Page<>(sysDeptSelectPageListFormDTO.getPageNo(), sysDeptSelectPageListFormDTO.getPageSize()),
				queryWrapper);
		return new PageResult<>(page.getTotal(), page.getRecords());
	}

	@Override
	public boolean validatorExist(SysDeptValidatorFormDTO sysDeptValidatorFormDTO) {
		QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(SysDept::getParentId, sysDeptValidatorFormDTO.getParentId());
		queryWrapper.lambda().eq(SysDept::getDeptName, sysDeptValidatorFormDTO.getDeptName());

		if (StringUtil.isNotBlank(sysDeptValidatorFormDTO.getId())) {
			queryWrapper.lambda().ne(SysDept::getId, sysDeptValidatorFormDTO.getId());
		}

		return this.count(queryWrapper) > 0;
	}

	@Override
	public boolean hasChildByDeptId(String id) {
		QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(SysDept::getDelFlag, 0);
		queryWrapper.lambda().eq(SysDept::getParentId, id);
		queryWrapper.last("limit 1");
		int result = this.count(queryWrapper);
		return result > 0 ? true : false;
	}

	@Override
	public List<SysDept> selectList(SysDeptSelectListFormDTO sysDeptSelectListFormDTO) {
		QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();

		if (StringUtil.isNotBlank(sysDeptSelectListFormDTO.getDeptName())) {
			queryWrapper.lambda().eq(SysDept::getDeptName, sysDeptSelectListFormDTO.getDeptName());
		}

		if (sysDeptSelectListFormDTO.getStatus() != null) {
			queryWrapper.lambda().eq(SysDept::getStatus, sysDeptSelectListFormDTO.getStatus());
		}

		queryWrapper.lambda().orderByAsc(SysDept::getOrderNum);
		return sysDeptMapper.selectList(queryWrapper);
	}
}
